package admin.controllers;

import helpers.CookieHelper;
import helpers.CrawlerHelper;
import helpers.GoogleStorageHelper;
import helpers.StringHelper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import entities.ApplicationTemplate;
import entities.Template;

import admin.models.ApplicationTemplateModel;

public class ApplicationTemplateRestoreServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		response.setContentType("text/html");
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		if (id == null || id.isEmpty()) {
			response.sendRedirect("/admin/apptemplate/list");
			return ;
		}
		
		ApplicationTemplateModel appTemplateModel = ApplicationTemplateModel.getInstance();
		ApplicationTemplate template = appTemplateModel.get(id);
		
		template = getContentTemplate(template, template.getId());
		
		appTemplateModel.insert(template);
		
		CookieHelper.setCookie(response, "success", "Restore default template " + template.getId() + " success");
		
		response.sendRedirect("/admin/apptemplate/edit/" + id);
	}
	
	private ApplicationTemplate getContentTemplate(ApplicationTemplate appTemplate, String templateId) {
		
		Template template = CrawlerHelper.getTemplate(templateId);
		/*TemplateModel templateModel = TemplateModel.getInstance();
		Template template = templateModel.get(templateId);*/
		
		try {
			/*
			 * Get list file by request to python application
			 */
			
			String[] splitFileName = GoogleStorageHelper.getListFile(templateId);
			/*
			 * read all files and compress
			 */
			for (String filename : splitFileName) {
				String content = "";
				filename = filename.replace("/gs/", "http://");
				
				if (filename.contains("home.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setHome(content);
				} 
				
				if (filename.contains("header.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setHeader(content);
				}
				
				if (filename.contains("footer.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setFooter(content);
				}
				
				if (filename.contains("layouts.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setLayout(content);
				}
				
				if (filename.contains("contact.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setContact(content);
				}
				
				if (filename.contains("static.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setStaticPage(content);
				}
				
				if (filename.contains("gsearch.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setSearch(content);
				}
				
				if (filename.contains("library.php")) {
					//content = GoogleStorageHelper.readFileGoogleStorage(filename);
					content = StringHelper.getContentFromUrl(filename);
					appTemplate.setLibrary(content);
				}
			}
			
			appTemplate.setBaseUrl(template.getBaseUrl());
			
		} catch (Exception e) {
			e.printStackTrace(System.err);
			System.out.println(
							"Error when trying to download template. Please try again later !");
		}
		
		
		return appTemplate;
	}
}
